# -*- coding: iso-8859-1 -*-
"""
@newfield project: Project 
@project: yatter
@newfield description: Description 
@description: Twitter Client, based on Python 2.5

@Author: U{Andreas del Galdo<mailto:andreas.delgaldo@mac.com>}
@summary: Creating the webrequests for communicating with Twitter. Every request is created
          in this Module.
"""

import yatter.tools.network
import yatter.tools.json_parser
import yatter.twitter_api.tweet

import logging

# set global path constants
url_publicTimeline  = "http://twitter.com/statuses/public_timeline.json" 
url_privateTimeline = "http://twitter.com/statuses/friends_timeline.json"
url_closeSession    = "http://twitter.com/account/end_session.json"

def createTweets(handler):
    try:
        logging.debug("twitter_api.interface.createTweets()")
    
        #parse JSON -> store the in tweet
        tweet_list = yatter.tools.json_parser.parseTweets(handler)
        
        #store tweets in tweet object
        tweetlist = []
        for tweets in tweet_list:
            
            myTweet = yatter.twitter_api.tweet.tweet()
            myTweet.setUser(tweets['user']['id'], tweets['user']['name'], tweets['user']['screen_name'], tweets['user']['description'], 
                            tweets['user']['location'], tweets['user']['profile_image_url'], tweets['user']['url'], 
                            tweets['user']['protected'], tweets['user']['followers_count'])        
            
            myTweet.setTweet(tweets['created_at'], tweets['id'], tweets['text'], tweets['source'], tweets['truncated'], 
                             tweets['in_reply_to_status_id'], tweets['in_reply_to_user_id'], tweets['favorited'])
            
            tweetlist.append(myTweet)
                   
            
        return tweetlist
    except:
        logging.error("twitter_api.interface.createTweets()::ERROR")
        return False


def closeSession():
    try:
        logging.debug("twitter_api.interface.closeSession()")
        handler = yatter.tools.network.createRequest(url_closeSession)
        return handler        
    except:
        logging.error("twitter_api.interface.closeSession()::ERROR")
        return False


def getPublicTimeline():
    try:
        logging.debug("twitter_api.interface.getPublicTimeline()")
        handler = yatter.tools.network.createRequest(url_publicTimeline)
        return handler
    except:
        logging.error("twitter_api.interface.getPublicTimeline()::ERROR")
        return False


def getPrivateTimeline(username,password):
    try:
        logging.debug("twitter_api.interface.getPrivateTimeline()")
        url_privateTimeline_username = url_privateTimeline.split('//')[0] + '//' + username + ":" + password + "@" + url_privateTimeline.split('//')[1]
        handler = yatter.tools.network.createRequest(url_privateTimeline_username)
        return handler        
    except:
        logging.error("twitter_api.interface.getPrivateTimeline()::ERROR")
        return False

